3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace Wikimedia\Rdbms
;
23 use InvalidArgumentException
;
26 * Class to handle database/prefix specification for IDatabase domains
28 class DatabaseDomain
{
29 /** @var string|null */
31 /** @var string|null */
36 /** @var string Cache of convertToString() */
37 private $equivalentString;
40 * @param string|null $database Database name
41 * @param string|null $schema Schema name
42 * @param string $prefix Table prefix
44 public function __construct( $database, $schema, $prefix ) {
45 if ( $database !== null && ( !is_string( $database ) ||
!strlen( $database ) ) ) {
46 throw new InvalidArgumentException( 'Database must be null or a non-empty string.' );
48 $this->database
= $database;
49 if ( $schema !== null && ( !is_string( $schema ) ||
!strlen( $schema ) ) ) {
50 throw new InvalidArgumentException( 'Schema must be null or a non-empty string.' );
52 $this->schema
= $schema;
53 if ( !is_string( $prefix ) ) {
54 throw new InvalidArgumentException( 'Prefix must be a string.' );
55 } elseif ( $prefix !== '' && substr( $prefix, -1, 1 ) !== '_' ) {
56 throw new InvalidArgumentException( 'A non-empty prefix must end with "_".' );
58 $this->prefix
= $prefix;
62 * @param DatabaseDomain|string $domain Result of DatabaseDomain::toString()
63 * @return DatabaseDomain
65 public static function newFromId( $domain ) {
66 if ( $domain instanceof self
) {
70 $parts = array_map( [ __CLASS__
, 'decode' ], explode( '-', $domain ) );
75 if ( count( $parts ) == 1 ) {
76 $database = $parts[0];
77 } elseif ( count( $parts ) == 2 ) {
78 list( $database, $prefix ) = $parts;
79 } elseif ( count( $parts ) == 3 ) {
80 list( $database, $schema, $prefix ) = $parts;
82 throw new InvalidArgumentException( "Domain '$domain' has too few or too many parts." );
85 if ( $database === '' ) {
89 if ( $schema === '' ) {
93 return new self( $database, $schema, $prefix );
97 * @return DatabaseDomain
99 public static function newUnspecified() {
100 return new self( null, null, '' );
104 * @param DatabaseDomain|string $other
105 * @return bool Whether the domain instances are the same by value
107 public function equals( $other ) {
108 if ( $other instanceof self
) {
110 $this->database
=== $other->database
&&
111 $this->schema
=== $other->schema
&&
112 $this->prefix
=== $other->prefix
116 return ( $this->getId() === $other );
120 * Check whether the domain $other meets the specifications of this domain
122 * If this instance has a null database specifier, then $other can have any database
123 * specified, including the null, and likewise if the schema specifier is null. This
124 * is not transitive like equals() since a domain that explicitly wants a certain
125 * database or schema cannot be satisfied by one of another (nor null). If the prefix
126 * is empty and the DB and schema are both null, then the entire domain is considered
127 * unspecified, and any prefix of $other is considered compatible.
129 * @param DatabaseDomain|string $other
133 public function isCompatible( $other ) {
134 if ( $this->isUnspecified() ) {
135 return true; // even the prefix doesn't matter
138 $other = self
::newFromId( $other );
141 ( $this->database
=== $other->database ||
$this->database
=== null ) &&
142 ( $this->schema
=== $other->schema ||
$this->schema
=== null ) &&
143 $this->prefix
=== $other->prefix
151 public function isUnspecified() {
153 $this->database
=== null && $this->schema
=== null && $this->prefix
=== ''
158 * @return string|null Database name
160 public function getDatabase() {
161 return $this->database
;
165 * @return string|null Database schema
167 public function getSchema() {
168 return $this->schema
;
172 * @return string Table prefix
174 public function getTablePrefix() {
175 return $this->prefix
;
181 public function getId() {
182 if ( $this->equivalentString
=== null ) {
183 $this->equivalentString
= $this->convertToString();
186 return $this->equivalentString
;
192 private function convertToString() {
193 $parts = [ (string)$this->database
];
194 if ( $this->schema
!== null ) {
195 $parts[] = $this->schema
;
197 if ( $this->prefix
!= '' ||
$this->schema
!== null ) {
198 // If there is a schema, then we need the prefix to disambiguate.
199 // For engines like Postgres that use schemas, this awkwardness is hopefully
200 // avoided since it is easy to have one DB per server (to avoid having many users)
201 // and use schema/prefix to have wiki farms. For example, a domain schemes could be
202 // wiki-<project>-<language>, e.g. "wiki-fitness-es"/"wiki-sports-fr"/"wiki-news-en".
203 $parts[] = $this->prefix
;
206 return implode( '-', array_map( [ __CLASS__
, 'encode' ], $parts ) );
209 private static function encode( $decoded ) {
212 $length = strlen( $decoded );
213 for ( $i = 0; $i < $length; ++
$i ) {
214 $char = $decoded[$i];
215 if ( $char === '-' ) {
217 } elseif ( $char === '?' ) {
227 private static function decode( $encoded ) {
230 $length = strlen( $encoded );
231 for ( $i = 0; $i < $length; ++
$i ) {
232 $char = $encoded[$i];
233 if ( $char === '?' ) {
234 $nextChar = $encoded[$i +
1] ??
null;
235 if ( $nextChar === 'h' ) {
238 } elseif ( $nextChar === '?' ) {
255 function __toString() {
256 return $this->getId();